package sequence;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class TimedSequence {
	private String name ;
	private long elapsedTime = 0;
	private LinkedList<TimedInt> list = new LinkedList<TimedInt>();
	
	public TimedSequence() {
	}

	public TimedSequence(String name) {
		this.name = name ;
	}

	public void add (long time, int id) {
		list.add(new TimedInt(time, id)) ;
	}
	
	public void prepare () {
		Collections.sort(list) ;
	}

	public int getLast(int delta) {
		elapsedTime += delta;
		TimedInt timedInt = null;
		while (!list.isEmpty() && elapsedTime >= list.getFirst().getTime())
			timedInt = list.removeFirst();

		if (timedInt != null)
			return timedInt.getValue();
		
		return -1;
	}
	
	public List<Integer> getAll(int delta) {
		elapsedTime += delta;
		LinkedList<Integer> result = new LinkedList<Integer> () ;
		while (!list.isEmpty() && elapsedTime >= list.getFirst().getTime())
			result.add(list.removeFirst().getValue()) ;

		return result;
	}

	public LinkedList<TimedInt> getList() {
		return list;
	}

	public void setList(LinkedList<TimedInt> list) {
		this.list = list;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
